import os
from clients.unetclient import unetclient
from client_tools.generate_json import foamData_json
import argparse
import cv2
import json
from flask import Flask
from flask import request
import copy
from gevent.pywsgi import WSGIServer
from gevent import monkey
import time
monkey.patch_all()
app = Flask(__name__)
app.config["DEBUG"] = False

parser = argparse.ArgumentParser(description="--server_url;--http_port;--gpu_id;co_no;--confidence")
parser.add_argument('-s','--server_url',type=str,default= '0.0.0.0:8001',help='服务器地址；默认 0.0.0.0:8001')
parser.add_argument('-p','--http_port',type=int,default=24575,help='接口地址；默认 8009')
parser.add_argument('-g','--gpu_id',type=int,default=1,help='0 是测试gpu，1 是正式服务；默认 1')
parser.add_argument('-n','--num',type=int,default=1,help='设置多client序列号；默认 1')
parser.add_argument('-c','--confidence',type=float,default=0.5,help='棚舍预警置信度；默认 0.5')

args = parser.parse_args()

# 超参
server_url = args.server_url
http_port = args.http_port #接口地址
gpu_id = args.gpu_id # 0 是测试gpu，1 是正式服务
co_no = args.num #设置多client序列号
confidence = args.confidence

# 获取client
foam_client = unetclient(url=server_url, model="unet",gpu_id = gpu_id,co_no = co_no)


@app.route("/foam_ratio", methods=['POST'])
def foamData_app():
    img_url = request.json.get("img_url").strip()  # url
    #result = foamData(img_url)
    ratio = result = foam_client.infer(
        img_type=1, input_img=img_url, confidence=0.5, test=0)
    
    return foamData_json(ratio)

@app.route('/health')
def health():
    health_response = {'status': 'UP'}
    return health_response

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=http_port,debug = False,processes=True)